package jrmob.yonyou.com.commonlib.app_config_tool.tool.file;

import android.content.Context;
import android.os.Environment;


import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import jrmob.yonyou.com.commonlib.app_config_tool.tool.base.LogUtils;

/**
 * 描述: 文件工具类
 * 
 * @下午5:41:37
 * @author zhangg
 */
public class FileUtils {

	public static final String CACHE = "cache";
	public static final String ICON = "icon";
	public static final String ROOT = "applicationroot";

	/**
	 *
	 * 
	 * @return
	 */
	public static File getIconDir(Context context) {
		return getDir(context,ICON);

	}

	/**
	 *
	 * 
	 * @return
	 */
	public static File getCacheDir(Context context) {
		return getDir(context,CACHE);
	}

	public static File getDir(Context context,String cache) {
		StringBuilder path = new StringBuilder();
		if (isSDAvailable()) {
			path.append(Environment.getExternalStorageDirectory()
					.getAbsolutePath());
			path.append(File.separator);// '/'
			path.append(ROOT);// /mnt/sdcard/GooglePlay
			path.append(File.separator);
			path.append(cache);// /mnt/sdcard/GooglePlay/cache

		} else {
			File filesDir = context.getCacheDir(); // cache
																// getFileDir
																// file
			path.append(filesDir.getAbsolutePath());// /data/data/com.itheima.googleplay/cache
			path.append(File.separator);// /data/data/com.itheima.googleplay/cache/
			path.append(cache);// /data/data/com.itheima.googleplay/cache/cache
		}
		File file = new File(path.toString());
		if (!file.exists() || !file.isDirectory()) {
			file.mkdirs();
		}
		return file;

	}

	private static boolean isSDAvailable() {
		return Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED);
	}

	/**
	 * 读文件
	 * 
	 * @param context
	 *            当前应用context
	 * @param filePath
	 *            "file/b.json"
	 * @return 文件内容
	 */
	public static String readAssetFile(Context context, String filePath) {
		StringBuffer Result = null;
		BufferedReader bufReader = null;
		try {
			InputStream open = context.getAssets().open(filePath);
			InputStreamReader inputReader = new InputStreamReader(open);
			bufReader = new BufferedReader(inputReader);
			String line = "";
			Result = new StringBuffer();
			while ((line = bufReader.readLine()) != null) {
				Result.append(line);
			}
		} catch (Exception e) {
			LogUtils.e( e);
			e.printStackTrace();
		} finally {
			closeStream(bufReader);
		}

		return Result.toString();

	}

	/***
	 * 读文件
	 * 
	 * @param context
	 *            当前应用context
	 * @param filePath
	 *            eg: /data/data/com.yonyou.jr.prp/cache/list.json
	 * @return 文件内容
	 */
	public static String readFile(Context context, String filePath) {
		BufferedReader bufReader = null;
		try {
			File file = new File(filePath);
			if (!file.exists()) {
				file.createNewFile();
			}
			return readFile(new FileInputStream(file));
		} catch (Exception e) {
			LogUtils.e( e);
			e.printStackTrace();
		} finally {
			closeStream(bufReader);
		}
		return "";
	}

	/**
	 * 读文件
	 * 
	 * @param stream
	 *            输入流
	 * @return
	 */
	public static String readFile(InputStream stream) {
		StringBuffer Result = null;
		BufferedReader bufReader = null;
		try {
			InputStreamReader inputReader = new InputStreamReader(stream);
			bufReader = new BufferedReader(inputReader);
			String line = "";
			Result = new StringBuffer();
			while ((line = bufReader.readLine()) != null) {
				Result.append(line);
			}
		} catch (Exception e) {
			LogUtils.e( e);
			e.printStackTrace();
		} finally {
			closeStream(bufReader);
		}
		return Result.toString();
	}

	/**
	 * 关闭流
	 * 
	 * @param stream
	 */
	public static void closeStream(Closeable stream) {
		try {
			if (stream != null) {
				stream.close();
				stream = null;
			}
		} catch (IOException e) {
			LogUtils.e(e);
			e.printStackTrace();
		}
	}

	/**
	 * 写文件
	 * 
	 * @param data
	 *            要保存的数据
	 * @param filePath
	 *            全文件路径+fileName
	 */
	public static void writeFile(String data, String filePath) {
		if (null == data || null == filePath || filePath.trim().length() == 0) {
			return;
		}
		FileWriter fileWritter = null;
		FileOutputStream outStream = null;
		try {
			File file = new File(filePath);
			if (!file.exists()) {
				file.createNewFile();
			}
			outStream = new FileOutputStream(file);
			outStream.write(data.getBytes());
		} catch (IOException e) {
			LogUtils.e(e);
			e.printStackTrace();
		} finally {
			closeStream(outStream);
			closeStream(fileWritter);
		}

	}

}